package com.weisy.signin.ui.activity;

import android.os.Handler;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.weisy.signin.R;
import com.weisy.signin.base.BaseAcativity;
import com.weisy.signin.bean.Student;
import com.weisy.signin.bean.Teacher;
import com.weisy.signin.comm.Comm;
import com.weisy.signin.utils.GsonUtils;
import com.weisy.signin.utils.SpUtils;
import com.weisy.signin.widget.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author weisy
 * @function 注册
 * @date 2019/3/21 10:47
 */
@Route(path = Comm.ROUTE_REGISTER)
public class RegisterActivity extends BaseAcativity {

    @BindView(R.id.edit_regist_id)
    EditText editId;
    @BindView(R.id.edit_regist_pass1)
    EditText editPass1;
    @BindView(R.id.edit_regist_pass2)
    EditText editPass2;

    @Autowired
    int userType;
    private String rgHint;

    private Student student;
    private Teacher teacher;

    @Override
    public void findView() {
        setContentView(R.layout.activity_register);
        ARouter.getInstance().inject(this);
    }

    @Override
    public void initView() {
        if (userType == 1) {
            editId.setHint("请输入学生学号");
            rgHint = "学生学号";
        } else {
            editId.setHint("请输入教师工号");
            rgHint = "教师工号";
            editId.setInputType(InputType.TYPE_CLASS_NUMBER);
        }
    }

    @Override
    public void initData() {

    }

    @OnClick({R.id.btn_register_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_register_submit:
                register();
                break;
        }
    }

    private void register() {
        String id = editId.getText().toString().trim();
        String pass1 = editPass1.getText().toString().trim();
        String pass2 = editPass2.getText().toString().trim();
        if (TextUtils.isEmpty(id)) {
            ToastUtils.show(rgHint + "不能为空");
            return;
        }
        if (TextUtils.isEmpty(pass1) || TextUtils.isEmpty(pass2)) {
            ToastUtils.show(rgHint + "密码不能为空");
            return;
        }
        if (!pass1.equals(pass2)) {
            ToastUtils.show("两次输入密码不一致");
            return;
        }
        final LoadingDialog loadingDialog = new LoadingDialog.Builder(context)
                .setBgDark(true)
                .setMsg("注册中...")
                .create();
        loadingDialog.show();
        if (userType == 1) {
            student = new Student();
            student.setStudentId(id);
            student.setPassword(pass1);
            SpUtils.put(context, Comm.SP_PERION, GsonUtils.bean2Str(student));
        } else {
            teacher = new Teacher();
            teacher.setTeachId(id);
            teacher.setPassword(pass1);
            SpUtils.put(context, Comm.SP_PERION, GsonUtils.bean2Str(teacher));
        }
        SpUtils.put(context, Comm.SP_TYPE, userType);
        new Handler().postDelayed(() -> {
            if (loadingDialog.isShowing()) {
                loadingDialog.dismiss();
            }

            new LoadingDialog.Builder(context)
                    .setBgDark(true)
                    .setType(LoadingDialog.TYPE_SUCCESS)
                    .setOnDismissListener((dialog, which) -> {
                        finish();
                    })
                    .setMsg("注册成功")
                    .create().show();
        }, 2000);
    }
}
